package cn.itcast.searchapi;

import cn.itcast.documentapis.esClient;
import org.elasticsearch.action.search.MultiSearchResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.jupiter.api.Test;

/**
 * @Description TODO :
 * @Author: zj
 * @Date: 16:28 2019/3/8
 * @Version 1.0
 **/
public class MultiSearchAPI {

    @Test
    public void multiSearch() {
        TransportClient client = esClient.getClient();

        SearchRequestBuilder srb1 = client.prepareSearch()
                .setQuery(QueryBuilders.queryStringQuery("male2"))
                .setSize(1);

        SearchRequestBuilder srb2 = client.prepareSearch()
                .setQuery(QueryBuilders.matchQuery("name", "kimchy"))
                .setSize(1);

        MultiSearchResponse items = client.prepareMultiSearch()
                .add(srb1)
                .add(srb2)
                .get();

        long nbHits = 0;

        for (MultiSearchResponse.Item item : items.getResponses()) {
            SearchResponse response = item.getResponse();
            System.out.println(response.toString());
            nbHits += response.getHits().getTotalHits();
        }
        System.out.println(nbHits);
    }
}
